package com.zhuidian.mothernote.global;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;
import androidx.core.app.ActivityOptionsCompat;

import com.base.activity.AbstractBaseActivity;
import com.base.activity.DragPhotoActivity;
import com.base.presenter.BasePresenter;
import com.base.presenter.BaseView;
import com.base.util.PopUtil;
import com.base.util.ScreenUtil;
import com.zhuidian.mothernote.R;

import java.util.ArrayList;
import java.util.List;


public abstract class BaseActivity<T extends BasePresenter> extends AbstractBaseActivity implements BaseView {

    protected T mPresenter;
    private RelativeLayout base_rl_top;
    private LinearLayout base_ll_left;
    private ImageView base_iv_back;
    private LinearLayout base_ll_middle;
    private TextView base_tv_title;
    private LinearLayout base_ll_right;
    private FrameLayout fl_content;
    private View contentView;
    private View emptyView;
    private ImageView TopRightImageView;

    @Override
    protected int inflateBaseLayoutId() {
        return R.layout.activity_base;
    }


    @Override
    protected void initBaseView(View view) {
        base_rl_top = (RelativeLayout) view.findViewById(R.id.base_rl_top);
        base_ll_left = (LinearLayout) view.findViewById(R.id.base_ll_left);
        base_iv_back = (ImageView) view.findViewById(R.id.base_iv_back);
        base_ll_middle = (LinearLayout) view.findViewById(R.id.base_ll_middle);
        base_tv_title = (TextView) view.findViewById(R.id.base_tv_title);
        base_ll_right = (LinearLayout) view.findViewById(R.id.base_ll_right);
        base_iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        fl_content = (FrameLayout) view.findViewById(R.id.fl_content);
        contentView = LayoutInflater.from(context).inflate(inflateMainLayoutId(), null);
        fl_content.addView(contentView);
        emptyView = LayoutInflater.from(context).inflate(R.layout.empty_view, null);
        TextView tv_empty = (TextView) (emptyView.findViewById(R.id.tv_empty));
        tv_empty.setText(R.string.network_error);
        fl_content.addView(emptyView);
        emptyView.setVisibility(View.GONE);

    }

    @Override
    protected void initValue() {

    }


    @Override
    public void onClick(View view) {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.onDestroy();
    }

    /**
     * 公共布局头部，默认可见，设置不可见
     */
    protected void setTopGone() {
        base_rl_top.setVisibility(View.GONE);
    }

    /**
     * 隐藏头部左边返回键
     */
    protected void setTopLeftGone() {
        base_iv_back.setVisibility(View.GONE);
    }


    public void setTopTitle(@StringRes int topTitleRes) {
        base_tv_title.setText(getString(topTitleRes));
    }

    public void setTopTitle(String topTitle) {
        base_tv_title.setText(topTitle);
    }


    @Override
    public void showMsg(String message) {
        PopUtil.toast(context, message);
    }

    /**
     * 设置顶部左边里面的控件,如果有多个控件，最好是inflate整个多控件的布局进去，当成一个整体
     *
     * @param view
     */
    protected void setTopLeftView(View view) {
        base_ll_left.removeAllViews();
        base_ll_left.addView(view);
    }

    /**
     * 设置顶部中间里面的控件,如果有多个控件，最好是inflate整个多控件的布局进去，当成一个整体
     *
     * @param view
     */
    protected void setTopMiddleView(View view) {
        base_ll_middle.removeAllViews();
        base_ll_middle.addView(view);
    }

    /**
     * 设置顶部右边里面的控件,如果有多个控件，最好是inflate整个多控件的布局进去，当成一个整体
     *
     * @param view
     */
    protected void setTopRightView(View view) {
        base_ll_right.removeAllViews();
        base_ll_right.addView(view);
    }


    /**
     * 隐藏头部右边
     */
    protected void setTopRightGone() {
        base_ll_right.setVisibility(View.GONE);

    }

    /**
     * 隐藏头部右边
     */
    protected void setTopRightVisible() {
        base_ll_right.setVisibility(View.VISIBLE);

    }

    protected View getTopRightView() {
        return base_ll_right.getChildAt(0);
    }

    protected View getTopLeftView() {
        return base_ll_left.getChildAt(0);
    }

    protected void setTopLeftImageViewId(@DrawableRes int resId, View.OnClickListener onClickListener) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(resId);
        imageView.setOnClickListener(onClickListener);
        int tenDp = getResources().getDimensionPixelOffset(R.dimen.ten_dp);
        int fifteenDp = getResources().getDimensionPixelOffset(R.dimen.fifteen_dp);
        imageView.setPadding(fifteenDp, tenDp, tenDp, tenDp);
        setTopLeftView(imageView);
    }

    protected void setTopRightImageViewId(@DrawableRes int resId, View.OnClickListener onClickListener) {
        TopRightImageView = new ImageView(context);
        TopRightImageView.setImageResource(resId);
        TopRightImageView.setOnClickListener(onClickListener);
        int tenDp = getResources().getDimensionPixelOffset(R.dimen.ten_dp);
        int fifteenDp = getResources().getDimensionPixelOffset(R.dimen.fifteen_dp);
        TopRightImageView.setPadding(tenDp, tenDp, fifteenDp, tenDp);
        setTopRightView(TopRightImageView);
    }

    protected ImageView getTopRightImageView() {
        return TopRightImageView;
    }

    protected void setTopLeftTextColorState(@ColorRes int colorid, @StringRes int resid, View.OnClickListener onClickListener) {
        TextView textView = new TextView(context);
        textView.setText(resid);
        textView.setTextSize(getResources().getDimensionPixelSize(R.dimen.text_size_12));
        textView.setTextColor(getResources().getColorStateList(colorid));
        textView.setOnClickListener(onClickListener);
        int tenDp = getResources().getDimensionPixelOffset(R.dimen.ten_dp);
        int fifteenDp = getResources().getDimensionPixelOffset(R.dimen.fifteen_dp);
        textView.setPadding(fifteenDp, tenDp, tenDp, tenDp);
        setTopLeftView(textView);
    }

    protected void setTopRightTextColorState(@ColorRes int colorid, @StringRes int resid, View.OnClickListener onClickListener) {
        TextView textView = new TextView(context);
        textView.setText(resid);
        textView.setTextColor(getResources().getColorStateList(colorid));
        textView.setOnClickListener(onClickListener);
        int tenDp = getResources().getDimensionPixelOffset(R.dimen.ten_dp);
        int fifteenDp = getResources().getDimensionPixelOffset(R.dimen.fifteen_dp);
        textView.setPadding(fifteenDp, tenDp, tenDp, tenDp);
        setTopRightView(textView);
    }

    protected void setTopRightViewWithPadding(View view) {
        int tenDp = getResources().getDimensionPixelOffset(R.dimen.ten_dp);
        int fifteenDp = getResources().getDimensionPixelOffset(R.dimen.fifteen_dp);
        view.setPadding(tenDp, tenDp, fifteenDp, tenDp);
        setTopRightView(view);
    }

    /**
     * 打开一个Fragment
     *
     * @param fragment
     */
    public void startFragment(Fragment fragment) {
        fragmentStackManager.addFragment(R.id.fl_fragment, fragment);
    }

    //设置状态栏
    public void setStatusBar(Window window) {
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ViewGroup decorViewGroup = (ViewGroup) window.getDecorView();
        View statusBarView = new View(window.getContext());
        int statusBarHeight = ScreenUtil.getStatusBarHeight(window.getContext());
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, statusBarHeight);
        params.gravity = Gravity.TOP;
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundResource(R.color.c3);
        decorViewGroup.addView(statusBarView);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void startActivityWithAnimation(View view, Intent intent) {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context, view, "shareView");
        startActivity(intent, optionsCompat.toBundle());
//        }
    }

    public void startActivityWithAnimation1(View view, Intent intent) {
        int[] locations = new int[2];
        view.getLocationOnScreen(locations);
        intent.putExtra("x", locations[0]);
        intent.putExtra("y", locations[1]);
        intent.putExtra("width", view.getWidth());
        intent.putExtra("hight", view.getHeight());
        startActivity(intent);
        overridePendingTransition(0, 0);
    }

    public void jumpToDragPhotoViewActivity(View imageView, String imagePath, int posiiton, int resId) {
        ArrayList<String> iamgePaths = new ArrayList<>();
        iamgePaths.add(imagePath);
        jumpToDragPhotoViewActivity(imageView, iamgePaths, 0, R.mipmap.default_pic_two);
    }


    public void jumpToDragPhotoViewActivity(View imageView, ArrayList<String> imagePaths, int posiiton, int resId) {
        Intent intent = new Intent(context, DragPhotoActivity.class);
        int location[] = new int[2];

        imageView.getLocationOnScreen(location);
        intent.putExtra("left", location[0]);
        intent.putExtra("top", location[1]);
        intent.putExtra("height", imageView.getHeight());
        intent.putExtra("width", imageView.getWidth());
        intent.putStringArrayListExtra(DragPhotoActivity.IMAGE_PATH, imagePaths);
        intent.putExtra(DragPhotoActivity.DEFAULT_RESID, resId);
        intent.putExtra(DragPhotoActivity.POSITION, posiiton);
        startActivity(intent);
        overridePendingTransition(0, 0);
    }

    public void jumpToDragPhotoViewActivity(List<View> imageViews, ArrayList<String> imagePaths, int posiiton, int resId) {
        Intent intent = new Intent(context, DragPhotoActivity.class);
        ArrayList<Float> lefts = new ArrayList<>();
        ArrayList<Float> righs = new ArrayList<>();
        ArrayList<Float> heights = new ArrayList<>();
        ArrayList<Float> widths = new ArrayList<>();
        for (View imageView : imageViews) {
            int location[] = new int[2];
            imageView.getLocationOnScreen(location);
            lefts.add((float) location[0]);
            righs.add((float) location[1]);
            heights.add((float) imageView.getHeight());
            widths.add((float) imageView.getWidth());
        }
        intent.putExtra("left", lefts);
        intent.putExtra("top", righs);
        intent.putExtra("height", heights);
        intent.putExtra("width", widths);
        intent.putStringArrayListExtra(DragPhotoActivity.IMAGE_PATH, imagePaths);
        intent.putExtra(DragPhotoActivity.DEFAULT_RESID, resId);
        intent.putExtra(DragPhotoActivity.POSITION, posiiton);
        startActivity(intent);
        overridePendingTransition(0, 0);
    }


    /**
     * 结束一个Fragment
     */
    public void finishFragment() {
        fragmentStackManager.popFragment();
    }

    @Override
    public MyApplication getMyApplication() {
        return (MyApplication) getApplication();
    }

    @Override
    public void onError() {
        if (emptyView.getVisibility() == View.GONE) {
            emptyView.setVisibility(View.VISIBLE);
            contentView.setVisibility(View.GONE);
        }
    }
}
